package com.gzdh.dishrecognition.ui.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.NonNull;

import com.dhkj.web.bean.MacMainpSbruanDinner;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.greendao.dao.BmcmEntityDao;
import com.greendao.dao.MacMainpSbruanDinnerEntityDao;
import com.gzdh.dishrecognition.AppContext;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.entity.BmcmEntity;
import com.gzdh.dishrecognition.entity.MacMainpSbruanDinnerEntity;
import com.gzdh.dishrecognition.util.SystemUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SetBmcmDialog extends Dialog {
    private Window window = null;

    private Context mContext;

    private TextView startTimeBut;

    private TextView endTimeTv;

    private TextView nameTv;

    private TextView okBut;

    private BmcmEntityDao bmcmEntityDao;

    private MacMainpSbruanDinnerEntityDao macMainpSbruanDinnerEntityDao;

    private String bm;

    private ResListener resListener;

    public void setResListener(ResListener resListener) {
        this.resListener = resListener;
    }

    public SetBmcmDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bmcm_set);
        mContext = this.getContext();
        //点击dialog以外的空白处是否隐藏
//        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
        //设置窗口显示
        windowDeploy();
    }

    private void initView() {
        bmcmEntityDao = AppContext.getInstance().getDaoSession().getBmcmEntityDao();
        macMainpSbruanDinnerEntityDao = AppContext.getInstance().getDaoSession().getMacMainpSbruanDinnerEntityDao();
        startTimeBut = findViewById(R.id.startTimeBut);
        endTimeTv = findViewById(R.id.endTimeTv);
        nameTv = findViewById(R.id.nameTv);
        okBut = findViewById(R.id.okBut);


    }

    private void initData() {

    }

    private void initEvent() {
        startTimeBut.setOnClickListener(view -> {
            timeClick(0);
        });
        endTimeTv.setOnClickListener(view -> {
            timeClick(1);
        });
        nameTv.setOnClickListener(view -> {
            List<BmcmEntity> bmcmEntityList = bmcmEntityDao.loadAll();
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("请选择餐次");
            final Map<String, String> map = new LinkedHashMap<>();
            for (BmcmEntity goo : bmcmEntityList) {
                map.put(goo.getName(), goo.getBm());
            }

            final String[] keysTemp = new String[bmcmEntityList.size()];
            final String[] keys = map.keySet().toArray(keysTemp);
            builder.setItems(keys, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    bm = map.get(keys[which]);
                    nameTv.setText(keys[which]);
                }
            });
            builder.show();
        });

        okBut.setOnClickListener(view -> {
            String startTime = startTimeBut.getText().toString();
            String endTime = endTimeTv.getText().toString();

            RequestUtils.addSbruanDinner(mContext, SystemUtils.getSerialNumber(), bm, startTime, endTime, new MyObserverRes<List<MacMainpSbruanDinner>>(mContext) {
                @Override
                public void onSuccess(List<MacMainpSbruanDinner> result) {
                    if (result.size() > 0) {
                        macMainpSbruanDinnerEntityDao.deleteAll();
                        List<MacMainpSbruanDinnerEntity> macMainpSbruanDinnerEntities = new ArrayList<>();
                        for (MacMainpSbruanDinner goo : result) {
                            MacMainpSbruanDinnerEntity macMainpSbruanDinnerEntity = new MacMainpSbruanDinnerEntity();
                            macMainpSbruanDinnerEntity.setDinner(goo.getDinner());
                            macMainpSbruanDinnerEntity.setStartTime(goo.getStartTime());
                            macMainpSbruanDinnerEntity.setEndTime(goo.getEndTime());
                            macMainpSbruanDinnerEntity.setDeviceId(goo.getDeviceId());
                            macMainpSbruanDinnerEntity.setId(goo.getId());
                            macMainpSbruanDinnerEntities.add(macMainpSbruanDinnerEntity);
                        }
                        macMainpSbruanDinnerEntityDao.insertInTx(macMainpSbruanDinnerEntities);
                    }
                    if (resListener != null) {
                        resListener.show();
                    }

                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });

        });
    }

    private void windowDeploy() {
        window = getWindow();
        window.setGravity(Gravity.CENTER); //设置窗口显示位置
//        window.setWindowAnimations(R.style.dialogWindowAnim); //设置窗口弹出动画
    }

    public void timeClick(int type) {
        Calendar calendar = Calendar.getInstance();
        TimePickerDialog dialog = new TimePickerDialog(mContext, AlertDialog.THEME_HOLO_LIGHT,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hour, int minute) {
                        //获取选中时间进行相应的业务实现
//                        Toast.makeText(mContext,
//                                hour + "小时" + minute + "分钟",
//                                Toast.LENGTH_SHORT).show();

                        if (type == 0) {
                            startTimeBut.setText(String.format("%02d", hour) + ":" + String.format("%02d", minute));
                        } else {
                            endTimeTv.setText(String.format("%02d", hour) + ":" + String.format("%02d", minute));
                        }

                    }
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true); //true为24小时制

        dialog.show();
    }


    public interface ResListener {
        void show();
    }
}
